package xyz.yuanjilu.plug.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.riversoft.weixin.common.message.Media;
import com.riversoft.weixin.common.message.MsgType;
import com.riversoft.weixin.common.message.XmlMessageHeader;
import com.riversoft.weixin.common.message.xml.ImageXmlMessage;
import com.riversoft.weixin.common.message.xml.TextXmlMessage;
import com.riversoft.weixin.common.request.TextRequest;

import xyz.yuanjilu.entity.WeixinConfigEntity;
import xyz.yuanjilu.entity.WeixinKeywordEntity;
import xyz.yuanjilu.plug.MPPlug;
import xyz.yuanjilu.service.WeixinKeywordService;

@Component
public class KeyWordMPPlugImpl implements MPPlug{
	@Autowired
	private WeixinKeywordService weixinKeywordService;
	
	
	@Override
	public boolean superWeixinCode(WeixinConfigEntity weixinConfig) {
		if (CollectionUtils.isNotEmpty(this.getWeixinKeywordEntityByWeixinId(weixinConfig.getId()))) {
			return true;
		}
		return false;
	}
	
	

	@Override
	public boolean superXmlMessageHeader(XmlMessageHeader xmlRequest, WeixinConfigEntity weixinConfig) {
		String keyWord="";
		if (xmlRequest instanceof TextRequest) {
			TextRequest request = (TextRequest) xmlRequest;
			keyWord=request.getContent();
		}else {
			return false;
		}
		
		List<WeixinKeywordEntity> weixinKeywords = this.getWeixinKeywordEntityByWeixinId(weixinConfig.getId());
		for (WeixinKeywordEntity weixinKeywordEntity : weixinKeywords) {
			if (weixinKeywordEntity.getKeyword().indexOf(keyWord)>-1) {
				return true;
			}
		}
		return false;
	}



	@Override
	public XmlMessageHeader handle(String weixinUserId, XmlMessageHeader xmlRequest,XmlMessageHeader xmlResponse, WeixinConfigEntity weixinConfig) {
		String keyWord="";
		if (xmlRequest instanceof TextRequest) {
			TextRequest request = (TextRequest) xmlRequest;
			keyWord=request.getContent();
		}
		if (xmlResponse instanceof TextXmlMessage) {
			TextXmlMessage response = (TextXmlMessage) xmlResponse;
			String text="\n";
			List<WeixinKeywordEntity> weixinKeywordEntityByWeixinId = this.getWeixinKeywordEntityByWeixinId(weixinConfig.getId());
			for (WeixinKeywordEntity weixinKeywordEntity : weixinKeywordEntityByWeixinId) {
				String keywordmessage = weixinKeywordEntity.getKeyword();
				if (keywordmessage.indexOf(keyWord)>-1) {
					if (MsgType.image.name().equals(weixinKeywordEntity.getContentType())) {
						ImageXmlMessage	message =new ImageXmlMessage();
						message.setToUser(weixinUserId);
						message.setMedia(new Media(weixinKeywordEntity.getContentInfo()));
						return message;
					}
					text+=weixinKeywordEntity.getContentInfo();
				}
			
			}
			response.setContent(text);
			return response;
		}else{
			return xmlResponse;
		}
	}


	@Override
	public String getTextMenu(WeixinConfigEntity weixinConfig) {
		List<WeixinKeywordEntity> weixinKeywords = this.getWeixinKeywordEntityByWeixinId(weixinConfig.getId());
		String text="";
		for (WeixinKeywordEntity weixinKeywordEntity : weixinKeywords) {
			if (1==weixinKeywordEntity.getHintFlag()) {
				text+="\n【"+weixinKeywordEntity.getKeyword()+"】,";
			}
			
		}
		return text;
	}

	@Override
	public String getDescribe(WeixinConfigEntity weixinConfig) {
		return "支持常见信息查询如回复关键字"+this.getTextMenu(weixinConfig);
	}

	@Override
	public String getName(WeixinConfigEntity weixinConfig) {
		return "关键字查询";
	}
		
	List<WeixinKeywordEntity> getWeixinKeywordEntityByWeixinId(long weixinId){
		Map<String, Object> param = new HashMap<String,Object>();
		param.put("weixinId", weixinId);
		List<WeixinKeywordEntity> queryList = weixinKeywordService.queryList(param);
		return queryList;
	}
		
}
